//
//  ViewController.swift
//  Keepscores
//
//  Created by stu on 2019/9/11.
//  Copyright © 2019年 Xiang HongFei. All rights reserved.
//

import UIKit
struct Revo {
    var idName:Int
    var n:Int
}

class ViewController: UIViewController {
    @IBOutlet weak var redScore: UILabel!
    var redn = 0
    @IBOutlet weak var blueScore: UILabel!
    var bluen = 0
    var revo = [Revo]()
    var revo_n = Revo(idName: 0, n: 0)
    var rn = -1
    
    @IBAction func red2(_ sender: UIButton) {
        redn += 2
        redScore.text = "\(redn)"
        rn = rn + 1
        revo_n.idName = 1
        revo_n.n = 2
        revo.append(revo_n)
    }
    @IBAction func red3(_ sender: UIButton) {
        redn += 3
        redScore.text = "\(redn)"
        rn = rn + 1
        revo_n.idName = 1
        revo_n.n = 3
        revo.append(revo_n)
    }
    
    @IBAction func blue2(_ sender: UIButton) {
        bluen += 2
        blueScore.text = "\(bluen)"
        rn = rn + 1
        revo_n.idName = 2
        revo_n.n = 2
        revo.append(revo_n)
    }
    @IBAction func blue3(_ sender: UIButton) {
        bluen += 3
        blueScore.text = "\(bluen)"
        rn = rn + 1
        revo_n.idName = 2
        revo_n.n = 3
        revo.append(revo_n)
    }
    @IBAction func red_revo(_ sender: UIButton) {
        if rn >= 0 {
            if revo[rn].idName == 1 {
                redn = redn - revo[rn].n
                revo.remove(at: rn)
                rn = rn - 1
                redScore.text = "\(redn)"
            } else{
                bluen  = bluen - revo[rn].n
                revo.remove(at: rn)
                rn = rn - 1
                blueScore.text = "\(bluen)"
            }
        } else {
            let alert = UIAlertController(title: "操作提示", message: "分数已清空，无法回退", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
            present(alert, animated: true, completion: nil)
        }
    }
    @IBAction func reset(_ sender: UIButton) {
        redn = 0
        bluen = 0
        rn = -1
        revo.removeAll()
        redScore.text = "\(redn)"
        blueScore.text = "\(bluen)"
        let alert = UIAlertController(title: "提示", message: "分数已重置", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }
}

